package leetcode.code1143;


public class Solution extends Solution1143 {

	@Override
	public int longestCommonSubsequence(String text1, String text2) {
		char[] chars1 = text1.toCharArray();
		char[] chars2 = text2.toCharArray();
		int[][] ans = new int[chars1.length][chars2.length];
		for (int i = 0; i < chars1.length; i++) {
			for (int j = 0; j < chars2.length; j++) {
				if (i == 0 || j == 0) {
					ans[i][j] = chars1[i] == chars2[j] ? 1
							: (i == 0 && j != 0) ? ans[i][j - 1] : (j == 0 && i != 0) ? ans[i - 1][j] : 0;
				} else {
					ans[i][j] = chars1[i] == chars2[j] ? ans[i - 1][j - 1] + 1 : Math.max(ans[i - 1][j], ans[i][j - 1]);
				}
			}
		}
		return ans[chars1.length - 1][chars2.length - 1];
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
